<?php

declare(strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

class Api extends Command
{
	protected function configure()
	{
		// 指令配置
		$this->setName('Api')->setDescription('api内容收集入库');
	}

	protected function execute(Input $input, Output $output)
	{
		// $this->daxue();
		// $this->renshe();
		// $this->sister_video();
		$this->story();
	}

	/**
	 * [story 故事大全]
	 * @url https://www.mxnzp.com/doc/detail?id=38
	 * @since  2025-08-20
	 * @author Dai哥
	 * @return [type]     [description]
	 */
	public function story()
	{
		# ea_api_story
		$type_list = [
			// ['type_id' => 1, 'name' => '睡前故事'],
			// ['type_id' => 2, 'name' => '儿童故事'],
			['type_id' => 3, 'name' => '格林童话'],
			['type_id' => 5, 'name' => '民间故事'],
			['type_id' => 6, 'name' => '益智故事'],
			['type_id' => 7, 'name' => '历史故事'],
			['type_id' => 8, 'name' => '寓言故事'],
			['type_id' => 9, 'name' => '经典童话'],
			['type_id' => 10, 'name' => '成语故事'],
			['type_id' => 11, 'name' => '童话作文'],
		];

		foreach ($type_list as $key => $value) {
			
			$data = [
				'app_id'     => 'sorolgopkimmfxtm',
				'app_secret' => 'VnEKHmvqOsdB6mK4NLDuBMQSoSUvx6FK',
				'type_id'    => $value['type_id'],
				'page'       => 14,
			];

			$this->getStoryList($data);
		}

		dump('End:完成');exit;
	}

	/**
	 * [getStoryList 获取故事列表]
	 * @since  2025-08-20
	 * @author Dai哥
	 * @return [type]     [description]
	 */
	public function getStoryList($data)
	{
		$url = "https://www.mxnzp.com/api/story/list?app_id=sorolgopkimmfxtm&app_secret=VnEKHmvqOsdB6mK4NLDuBMQSoSUvx6FK&type_id={$data['type_id']}&page={$data['page']}";

		$list = $this->curlGet($url);

		if (isset($list['data']) && count($list['data']) > 0) {

			foreach ($list['data'] as $key => $value) {
				
				$value['typeId'] = $data['type_id'];

				$cks = Db::table('ea_api_story')->where('storyId', $value['storyId'])->count();

				if (!$cks) {

					$value['create_time'] = time();
					$value['update_time'] = time();

					Db::table('ea_api_story')->insert($value);

					dump($value['type'].' => '. $value['storyId'].' : '.$value['title']. ' '.date('Y-m-d H:i:s'));
				}
			}

			$data['page'] = (int)($data['page'] + 1);

			dump('睡眠1s, 执行下一页: '.$data['page']);

			sleep(2);

			return $this->getStoryList($data);
		} 

		dump($list);
		
		// return [];
	}

	public function curlGet($url)
	{
		$curl = curl_init();

		curl_setopt_array($curl, [
		  CURLOPT_URL => $url,
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 30,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "GET",
		]);

		$response = curl_exec($curl);
		$err = curl_error($curl);

		curl_close($curl);

		if ($err) {
		  
		  	echo "cURL Error #:" . $err;exit;

		} else {
		  	
		  	return json_decode($response, true);
		}
	}

	/**
	 * [daxue 中国大学] https://www.free-api.com/doc/666
	 * @since  2025-08-01
	 * @author Dai哥
	 * @return [type]     [description]
	 */
	public function daxue()
	{

		$list = Db::table('ea_api_daxue')->whereNull('founding')->select();
		// dump(count($list));exit;

		foreach ($list as $key => $value) {
			
			$curl = curl_init();

			curl_setopt_array($curl, [
				CURLOPT_URL => "https://api.52vmy.cn/api/query/daxue?daxue=".$value['name'],
				CURLOPT_RETURNTRANSFER => true,
				CURLOPT_ENCODING => "",
				CURLOPT_MAXREDIRS => 10,
				CURLOPT_TIMEOUT => 30,
				CURLOPT_CUSTOMREQUEST => "GET",
				CURLOPT_POSTFIELDS => "",
			]);

			$response = curl_exec($curl);
			$err = curl_error($curl);

			curl_close($curl);

			if ($err) {

				echo "cURL Error #:" . $err;

			} else {
				
				$info = json_decode($response, true);
				// dump($info);
				// continue;
				// 

				if (isset($info['code']) && $info['code'] == 200) {

					$arrs = [
						'founding'    => $info['data']['founding'],
						'area' 		  => $info['data']['area'],
						'encode' 	  => $info['data']['encode'],
						'address' 	  => $info['data']['address'],
						'intro' 	  => $info['data']['intro'],
						'detail' 	  => $info['data']['detail'],
						'update_time' => time(),
						'create_time' => time(),
					];

					Db::table('ea_api_daxue')->where('id', $value['id'])->update($arrs);

				} else {

					if ($info['data'] == '学校不存在') {

						$arrs = [
							'founding'    => '1970',
							'update_time' => time(),
						];

						Db::table('ea_api_daxue')->where('id', $value['id'])->update($arrs);
					}

					dump($info['data'].':'.$value['name']);

				}

				

				dump($key);
				sleep(1);

			}
		}

		dump('End:完成');
	}

	/**
	 * [renshe 随机人设]
	 * @url https://www.free-api.com/doc/647
	 * @since  2025-08-06
	 * @author Dai哥
	 * @return [type]     [description]
	 */
	public function renshe()
	{

		for ( $i = 1; $i < 5000; $i++ ) { 
			
			$curl = curl_init();

			curl_setopt_array($curl, [
				CURLOPT_URL => "https://api.kuleu.com/api/suiji_renshe",
				CURLOPT_RETURNTRANSFER => true,
				CURLOPT_MAXREDIRS => 10,
				CURLOPT_TIMEOUT => 30,
				CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
				CURLOPT_CUSTOMREQUEST => "GET",
			]);

			$response = curl_exec($curl);
			$err = curl_error($curl);

			curl_close($curl);

			if ($err) {

				dump($err);

			} else {

				if (!$response) {

					dump('没有数据'.date('Y-m-d H:i:s'));

					continue;
				}

				$md5key = md5($response);

				$ck = Db::table('ea_api_renshe')->field('id')->where('md5key', $md5key)->find();

				if (!$ck) {

					$res = Db::table('ea_api_renshe')->insert([
						'create_time' => time(),
						'update_time' => time(),
						'content' 	  => $response,
						'md5key' 	  => $md5key,
					]);
	
					dump($i.' <=> '.$res.' <=> '.date('Y-m-d H:i:s'));
				} else {

					dump($i.' <=> 存在了 <=> '.date('Y-m-d H:i:s'));
				}
			}
		}
	}

	/**
	 * [sister_video 高质量小姐姐视频]
	 * @url https://www.free-api.com/doc/649
	 * @since  2025-08-06
	 * @author Dai哥
	 * @return [type]     [description]
	 */
	public function sister_video()
	{

		for ( $i = 1; $i < 5000; $i++ ) { 
			
			$curl = curl_init();

			curl_setopt_array($curl, [
				CURLOPT_URL => "https://api.kuleu.com/api/MP4_xiaojiejie?type=json",
				CURLOPT_RETURNTRANSFER => true,
				CURLOPT_MAXREDIRS => 10,
				CURLOPT_TIMEOUT => 30,
				CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
				CURLOPT_CUSTOMREQUEST => "GET",
			]);

			$response = curl_exec($curl);
			$err = curl_error($curl);

			curl_close($curl);

			if ($err) {

				dump($err);

			} else {

				$response = json_decode($response, true);

				if (isset($response['code']) && $response['code'] == 200) {

					$ck = Db::table('ea_api_sister_video')->field('id')->where('path', $response['mp4_video'])->find();

					if (!$ck) {

						$res = Db::table('ea_api_sister_video')->insert([
							'create_time' => time(),
							'update_time' => time(),
							'path' 	  => $response['mp4_video'],
						]);
	
						dump($i.' <=> '.$res.' <=> '.date('Y-m-d H:i:s'));

					} else {

						dump($i.' <=> 存在了 <=> '.date('Y-m-d H:i:s'));
					}

				} else {

					dump($response);
				}
			}
		}
	}

	/**
	 * [views description]
	 * @since  2025-08-06
	 * @author Dai哥
	 * @return [type]     [description]
	 */
	public function views()
	{

	}
}
